/**
 * ReadMode 阅读模式插件
 *
 * new ReadMode().init('btnId', callback);
 */
define('pc/js/components/ReadMode/ReadMode', null, function(){

var setStyle = function(obj, styles) {
    for(var k in styles) {
        obj.style[k] = styles[k];
    }
};

var ReadMode = function() {
    this.doc = document;
    this.maskId = 'yreadmask';
    this.maskObj = null;
    this.headerObj = null;
    this.headerHtml = '阅读 ( ESC 退出 )';
    this.conObj = null;
};
ReadMode.prototype = {
    constructor : ReadMode,
    start : function() {
        var _this = this;

        this.initMask();
        this.initHeader();
        this.initContainer();
    },
    initMask : function() {
        this.maskObj = this.doc.createElement('div');
        this.maskObj.id = this.maskId;

        setStyle(this.maskObj, {
            position: 'fixed',
            zIndex: 1000,
            width: '100%',
            height: '100%',
            top: 0,
            left: 0,
            backgroundColor: '#fbfcf7',
            overflow: 'auto',
            fontFamily: '"SF Pro Text","SF Pro Icons","Helvetica Neue","Helvetica","Arial"',
            fontSize: '14px',
            color: '#333'
        });
    },
    initHeader : function() {
        this.headerObj = this.doc.createElement('div');
        this.headerObj.innerHTML = this.headerHtml;

        setStyle(this.headerObj, {
            position: 'fixed',
            width: '100%',
            top: '0',
            left: '0',
            height: '60px',
            lineHeight: '60px',
            textAlign: 'center',
            backgroundColor: '#f8faed',
            borderBottom: '1px solid #eee'
        });

        this.maskObj.appendChild(this.headerObj);
    },
    initContainer : function() {
        this.conObj = this.doc.createElement('div');

        setStyle(this.conObj, {
            width: '960px',
            margin: '0 auto',
            padding: '80px 0 20px 0',
            lineHeight: '1.8'
        });
        this.conObj.className = 'x-richcontent';

        this.maskObj.appendChild(this.conObj);
    },
    open : function() {
        if(!this.doc.getElementById(this.maskId)) {
            this.doc.body.appendChild(this.maskObj);
            this.doc.body.style.overflow = 'hidden';
        }
    },
    close : function(e) {
        if(e.keyCode != 27) return;
        if(!!this.doc.getElementById(this.maskId)) {
            this.doc.body.removeChild(this.maskObj);
            this.doc.body.style.overflow = 'auto';
        }
    },
    init : function(id, callback) {
        var _this = this;
        var obj = this.doc.getElementById(id);

        if(!obj) {
            return;
        }

        // 初始化 只执行一次
        this.start();

        // 监听 esc
        this.doc.onkeydown = function(e){
            e = e || window.event;
            _this.close(e);
        };
        // 打开
        obj.onclick = function(){
            _this.open();
            if(undefined != callback) {callback(_this.conObj);}
        };
        obj = null;
    }
};

return ReadMode;
});
